/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "translationconfig.h"
#include "src/platform.h"
#include <QtGui/QCheckBox>
#include <QtGui/QComboBox>
#include <QtGui/QLineEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QApplication>
#include <QtGui/QKeyEvent>
#include <QtCore/QEvent>
#include <QtCore/QSettings>


// Default values
const bool
TranslationConfig::m_default_auto_translate = false,
TranslationConfig::m_default_copy_text = false,
TranslationConfig::m_default_auto_detect = false,
TranslationConfig::m_default_simplify_result = false,
TranslationConfig::m_default_scan_selection = true,
TranslationConfig::m_default_modifier_enabled = false,
TranslationConfig::m_default_hotkey_enabled = false;

const int
TranslationConfig::m_default_modifier_index = 0;

const QKeySequence
TranslationConfig::m_default_hotkey = QKeySequence("");

TranslationConfig::TranslationConfig(QWidget *parent) :
    QWidget(parent)
{
    createUi();
    changeEvent(new QEvent(QEvent::LanguageChange));


    hotkeyLine->installEventFilter(this);
    connect(scanSelection, SIGNAL(toggled(bool)), enableModifier, SLOT(setEnabled(bool)));
    connect(enableModifier, SIGNAL(toggled(bool)), modifiersComboBox, SLOT(setEnabled(bool)));
    connect(enableHotkey, SIGNAL(toggled(bool)), hotkeyLine, SLOT(setEnabled(bool)));

    read();
}

TranslationConfig::~TranslationConfig() {
    delete autoTranslate;
    delete autoDetect;
    delete scanSelection;
    delete autoCopy;
    delete simplifyResult;
    delete enableModifier;
    delete enableHotkey;

    delete modifiersLayout;
    delete hotkeyLayout;

    delete mainLayout;

    delete modifiersComboBox;
    delete hotkeyLine;
}

bool TranslationConfig::getAutoTranslate() const {
    return autoTranslate->isChecked();
}

bool TranslationConfig::getAutoDetect() const {
    return autoDetect->isChecked();
}

#ifdef Q_WS_X11
bool TranslationConfig::getScanSelection() const {
    return scanSelection->isChecked();
}
#endif

bool TranslationConfig::getAutoCopy() const {
    return autoCopy->isChecked();
}

bool TranslationConfig::getSimplifyResult() const {
    return simplifyResult->isChecked();
}


bool TranslationConfig::getModifierEnabled() const {
    return enableModifier->isChecked();
}

QKeySequence TranslationConfig::getTranslationModifier() const {
    return QKeySequence(modifiersComboBox->currentText());
    // HACK: warning! It may be wrong hack!
}

bool TranslationConfig::getHotkeyEnabled() const {
    return enableHotkey->isChecked();
}

QKeySequence TranslationConfig::getTranslationHotkey() const {
    return QKeySequence(hotkeyLine->text());
}

void TranslationConfig::setAutoTranslate(bool b) {
    autoTranslate->setChecked(b);
}

void TranslationConfig::setAutoDetect(bool b) {
    autoDetect->setChecked(b);
}

void TranslationConfig::setScanSelection(bool b) {
    scanSelection->setChecked(b);
}

void TranslationConfig::save() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Translation");
    s.setValue("autoTranslate", autoTranslate->isChecked());
    s.setValue("autoDetect", autoDetect->isChecked());
    s.setValue("scanSelection", scanSelection->isChecked());
    s.setValue("autoCopy", autoCopy->isChecked());
    s.setValue("simplifyResult", simplifyResult->isChecked());
    s.setValue("enableModifier", enableModifier->isChecked());
    s.setValue("modifier", modifiersComboBox->currentIndex());
    s.setValue("enableHotkey", enableHotkey->isChecked());
    s.setValue("hotkey", hotkeyLine->text());
    s.endGroup();
}

void TranslationConfig::read() {
    QSettings s(Platform::getOrganizationName(), Platform::getApplicationName());
    s.beginGroup("Translation");

    autoTranslate->setChecked(s.value("autoTranslate", m_default_auto_translate).toBool());
    autoDetect->setChecked(s.value("autoDetect",  m_default_auto_detect).toBool());
    scanSelection->setChecked(s.value("scanSelection",m_default_scan_selection).toBool());
    autoCopy->setChecked(s.value("autoCopy", m_default_copy_text).toBool());
    simplifyResult->setChecked(s.value("simplifyResult", m_default_simplify_result).toBool());
    enableModifier->setChecked(s.value("enableModifier", m_default_modifier_enabled).toBool());
    modifiersComboBox->setCurrentIndex(s.value("modifier", m_default_modifier_index).toInt());
    enableHotkey->setChecked(s.value("enableHotkey", m_default_hotkey_enabled).toBool());
    hotkeyLine->setText(s.value("hotkey", m_default_hotkey).toString());

    s.endGroup();
}

void TranslationConfig::truncate() {
    autoTranslate->setChecked(m_default_auto_translate);
    autoDetect->setChecked(m_default_auto_detect);
    scanSelection->setChecked(m_default_scan_selection);
    autoCopy->setChecked(m_default_copy_text);
    simplifyResult->setChecked(m_default_simplify_result);
    enableModifier->setChecked(m_default_modifier_enabled);
    enableHotkey->setChecked(m_default_hotkey_enabled);
    modifiersComboBox->setCurrentIndex(m_default_modifier_index);
    hotkeyLine->setText(m_default_hotkey.toString());

}

void TranslationConfig::createUi() {
    autoTranslate = new QCheckBox(this);
    autoDetect = new QCheckBox(this);
    scanSelection = new QCheckBox(this);
    autoCopy = new QCheckBox(this);
    simplifyResult = new QCheckBox(this);
    enableModifier = new QCheckBox(this);
    enableHotkey = new QCheckBox(this);

    modifiersLayout = new QHBoxLayout;
    hotkeyLayout=  new QHBoxLayout;

    mainLayout = new QVBoxLayout;

    modifiersComboBox = new QComboBox(this);
    hotkeyLine = new QLineEdit(this);

    modifiersLayout->addWidget(enableModifier);
    modifiersLayout->addWidget(modifiersComboBox);
    modifiersLayout->addStretch();

    hotkeyLayout->addWidget(enableHotkey);
    hotkeyLayout->addWidget(hotkeyLine);
    hotkeyLayout->addStretch();

    mainLayout->addWidget(autoTranslate);
    mainLayout->addWidget(autoDetect);
    mainLayout->addWidget(autoCopy);
    mainLayout->addWidget(simplifyResult);
    mainLayout->addWidget(scanSelection);

    mainLayout->addLayout(modifiersLayout);
    mainLayout->addLayout(hotkeyLayout);
    mainLayout->addStretch();
    this->setLayout(mainLayout);
}

void TranslationConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
	autoTranslate->setText(QApplication::tr("Translate by input text"));
	autoDetect->setText(QApplication::tr("Auto Detect source language"));
	scanSelection->setText(QApplication::tr("Scan selection buffer"));
	autoCopy->setText(QApplication::tr("Copy Translation result"));
	simplifyResult->setText(QApplication::tr("Simplify Translation result"));
        enableModifier->setText(QApplication::tr("If modifier pressed"));
        enableHotkey->setText(QApplication::tr("When hotkey pressed"));
    }

}

bool TranslationConfig::eventFilter(QObject *obj, QEvent *event) {

    if (event->type() == QEvent::KeyPress) {
	QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
	int key = keyEvent->text().length() == 1 ? keyEvent->key() : 0;
	int mod = keyEvent->modifiers() & ~Qt::KeypadModifier;
	hotkeyLine->setText(QKeySequence(key + mod).toString());

	return true;
    }
  else
      return QObject::eventFilter(obj, event);

  return true;
}

